package cn.cena.pet.service.impl;

import cn.cena.basic.service.impl.BaseServiceImpl;
import cn.cena.basic.util.PageList;
import cn.cena.pet.domain.Pet;
import cn.cena.pet.domain.PetDetail;
import cn.cena.pet.mapper.PetDetailMapper;
import cn.cena.pet.mapper.PetMapper;
import cn.cena.pet.query.PetQuery;
import cn.cena.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    /**
     * 将处理后的t_search_master_msg信息存入pet表
     * @param pet
     */
    @Override
    public void add(Pet pet) {
        // 将信息存入pet表
        petMapper.save(pet);

        // 获取PetDetail信息对象
        PetDetail petDetail = pet.getDetail();
        // 将pet_id存入PetDetail对象
        petDetail.setPet_id(pet.getId());
        // 存入PetDetail表
        petDetailMapper.save(petDetail);

    }

    /**
     * 宠物上架
     * @param ids
     * @param stateOffsale
     */
    @Override
    public void onsale(List<Long> ids, Integer state) {

        // 通过state和id查找pet
       List<Pet> list = petMapper.findByIds(ids,Pet.STATE_OFFSALE);

       // 修改宠物上架时间
        Date date = new Date();
        list.forEach(pet -> {
           pet.setState(Pet.STATE_ONSALE);
           pet.setOnsaletime(date);
           petMapper.update(pet);
       });
    }

    /**
     * 宠物下架
     * @param ids
     * @param state
     */
    @Override
    public void offsale(List<Long> ids, Integer state) {
        // 通过state和id查找pet
        List<Pet> list = petMapper.findByIds(ids,Pet.STATE_ONSALE);

        // 修改宠物下架时间
        Date date = new Date();
        list.forEach(pet -> {
            pet.setState(Pet.STATE_OFFSALE);
            pet.setOffsaletime(date);
            petMapper.update(pet);
        });
    }

    /**
     * 查找以上架宠物
     * @param id
     * @param state
     * @return
     */
    @Override
    public Pet loadOnsaleById(Long id, Integer state) {
        Pet pet = petMapper.findOnsaleById(id, Pet.STATE_ONSALE);
        return pet;
    }
}
